/**
 * 
 */
package com.ailk.evaluation.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ailk.evaluation.common.PageResponse;
import com.ailk.evaluation.manager.AutoExecutableManager;
import com.ailk.evaluation.manager.ParameterUtils;
import com.anhorn.webshare.base.web.BaseAction;

/**
 * @author DerekHong
 *
 */
public abstract class SimpleOperationAction extends BaseAction{
	
	protected Map<String, String> getGetMethodParameters(HttpServletRequest request) {
		Map<String, String> params = new HashMap<String, String>();		
		String query = request.getQueryString();
		ParameterUtils.processParameters(params, query);
		return params;
		
	}
	
	//@Privilege("editor")
	public ActionForward list(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){	
		PageResponse pageResponse = getManager(request).pageQuery(this.getTableName(request), request);
		request.setAttribute("page", pageResponse);
		return mapping.findForward("list");	
	}
	
	//@Privilege("editor")
	public ActionForward find(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){	
		request.setAttribute("outputResult", getManager(request).find(this.getTableName(request), request));
		return mapping.findForward("find");	
	}
	
	/**
	 * 
	 * @deprecated
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	//@Privilege("editor")
	public ActionForward findMap(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){	
		return this.find(mapping, form, request, response);
	}

	//@Privilege("editor")
	public ActionForward create(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){	
		request.setAttribute("outputResult", getManager(request).save(this.getTableName(request), request));
		return mapping.findForward("create");	
	}
	
	//@Privilege("editor")
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){	
		request.setAttribute("outputResult", getManager(request).update(this.getTableName(request), request));
		return mapping.findForward("update");
	}
	
	//@Privilege("editor")
	public ActionForward delete(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){	
		request.setAttribute("outputResult", getManager(request).delete(this.getTableName(request),request));
		return mapping.findForward("delete");
	}
	
	/**
	 * @return
	 */
	protected abstract String getTableName(HttpServletRequest request);
	
	/**
	 * default manager
	 * @param request
	 * @return
	 */
	protected AutoExecutableManager getManager(HttpServletRequest request) {
		return ((AutoExecutableManager)getBean("autoExecutable"));	
	}
}
